extends Area2D

var velocity = -20
var allow_input = false
var jump = 0
var ground = 576
var death_fall_speed = 10

@onready var animationplayer = get_node("AnimationPlayer")
@onready var timer = get_node("Timer")
@onready var death_message = get_node("/root/main/death_label")


func _ready():
	timer.connect("timeout", set_jump)
	timer.start()
	Global.player_dead = false

func _process(delta):

	if (Global.player_dead):
		death_message.show()
		if position.y < ground:
			position.y += death_fall_speed
		else:
			position.y = ground
			if Input.is_action_just_pressed("space"):
				Global.player_dead = false
				get_tree().change_scene_to_file("res://scenes/main.tscn")
		return

	if Input.is_action_pressed("space"):
		velocity -= jump * delta
		animationplayer.play("flappying")
	else:
		velocity += 25 * delta
		animationplayer.play("RESET")

	position.y += velocity

func set_jump():
	jump = 45
	timer.stop()
